package com.algrithom.stropr;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution2 {
    
    public static void main(String[] args){
        char[] chs = new char[]{'a','a','b','b','c','c','c'};
        Solution2 solution2 = new Solution2();
        System.out.println(solution2.compress(chs));
    }
    
    public int compress(char[] chars){
        Map<Character,Integer> map = new HashMap<>();
        for (char ch : chars) {
            map.put(ch,map.getOrDefault(ch,0) + 1);
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<Character,Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                stringBuilder.append(entry.getKey());
            } else {
                stringBuilder.append(entry.getKey()).append(entry.getValue());
            }
        }
        chars = Arrays.copyOf(stringBuilder.toString().toCharArray(),stringBuilder.toString().length());
        return stringBuilder.toString().length();
    }
}
